package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.LeaveAdjustment;
import com.amsp.oa.oademo.service.LeaveAdjustmentService;
import com.amsp.oa.oademo.vo.LeaveAdjustmentVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "调休申请单管理")
@RestController
@RequestMapping("/api/leave-adjustment")
public class LeaveAdjustmentController {

    @Resource
    private LeaveAdjustmentService leaveAdjustmentService;

    @ApiOperation("获取调休申请单列表")
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getList(
            @ApiParam(value = "页码", required = true) @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页大小", required = true) @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "月份，格式：yyyy-MM") @RequestParam(required = false) String month,
            @ApiParam(value = "员工姓名") @RequestParam(required = false) String employeeName) {

        IPage<LeaveAdjustment> page = leaveAdjustmentService.getList(pageNum, pageSize, month, employeeName);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "获取列表成功");
        result.put("data", page);

        return ResponseEntity.ok(result);
    }

    @ApiOperation("获取调休申请单详情")
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getDetail(
            @ApiParam(value = "调休申请单ID", required = true) @PathVariable Long id) {

        LeaveAdjustmentVO detail = leaveAdjustmentService.getDetail(id);

        Map<String, Object> result = new HashMap<>();
        if (detail != null) {
            result.put("code", 200);
            result.put("message", "获取详情成功");
            result.put("data", detail);
        } else {
            result.put("code", 404);
            result.put("message", "未找到对应的调休申请单");
        }

        return ResponseEntity.ok(result);
    }

    @ApiOperation("保存或更新调休申请单")
    @PostMapping("/save")
    public ResponseEntity<Map<String, Object>> save(@RequestBody LeaveAdjustmentVO leaveAdjustmentVO) {
        boolean success = leaveAdjustmentService.saveOrUpdate(leaveAdjustmentVO);

        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "保存成功");
        } else {
            result.put("code", 500);
            result.put("message", "保存失败");
        }

        return ResponseEntity.ok(result);
    }

    @ApiOperation("删除调休申请单")
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(
            @ApiParam(value = "调休申请单ID", required = true) @PathVariable Long id) {

        boolean success = leaveAdjustmentService.delete(id);

        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "删除失败");
        }

        return ResponseEntity.ok(result);
    }

    @ApiOperation("批量删除调休申请单")
    @PostMapping("/batch-delete")
    public ResponseEntity<Map<String, Object>> batchDelete(@RequestBody List<Long> ids) {
        boolean success = leaveAdjustmentService.batchDelete(ids);

        Map<String, Object> result = new HashMap<>();
        if (success) {
            result.put("code", 200);
            result.put("message", "批量删除成功");
        } else {
            result.put("code", 500);
            result.put("message", "批量删除失败");
        }

        return ResponseEntity.ok(result);
    }
}